package com.example.jigsawpuzzle;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Environment;
import android.widget.ImageView;

public class Helper {
    public static List<ImageView> imageArray2List(ImageView[][] imageViews,
            int row, int column) {
        List<ImageView> iList = new ArrayList<ImageView>();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                iList.add(imageViews[i][j]);
            }
        }
        return iList;
    }

    public static ImageView[][] list2ImageArray(List<ImageView> list, int row,
            int column) {
        if (list == null)
            return new ImageView[0][0];
        if (list.size() != row * column)
            return new ImageView[0][0];
        ImageView[][] imageViews = new ImageView[row][column];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                imageViews[i][j] = list.get(i * row + j);
            }
        }
        return imageViews;
    }

    public static String getDiskCacheDir(Context context) {
        String cachePath = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            cachePath = context.getExternalCacheDir().getPath();
        } else {
            cachePath = context.getCacheDir().getPath();
        }
        cachePath += "/GameData/";
        File file = new File(cachePath);
        if (!file.exists())
            file.mkdirs();
        return cachePath;
    }

    @SuppressWarnings("unchecked")
    public static ImageView[][] getGameData4Local(Context context, int row,
            int column) {
        ImageView[][] imageViews = null;
        String dataPath = getDiskCacheDir(context) + "data.dat";
        File file = new File(dataPath);
        if (!file.exists()) {
            imageViews = new ImageView[0][0];
            return imageViews;
        }
        List<ImageView> list = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            list = (List<ImageView>) ois.readObject();
            imageViews = list2ImageArray(list, row, column);
            ois.close();
            fis.close();
        } catch (Exception e) {
            imageViews = new ImageView[0][0];
        }
        return imageViews;
    }

    public static void saveGameData2Local(ImageView[][] imageViews, int row,
            int column, Context context) {
        List<ImageView> list = imageArray2List(imageViews, row, column);
        String dataPath = getDiskCacheDir(context) + "data.dat";
        File file = new File(dataPath);
        if (file.exists()) {
            file.delete();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            FileOutputStream fis = new FileOutputStream(file);
            ObjectOutputStream ois = new ObjectOutputStream(fis);
            ois.writeObject(list);
            ois.close();
            fis.close();
        } catch (Exception e) {

        }
    }
}
